<template>
	<div>
        <!--<a-header :goBack="goBack" :goAddAddress="goAddAddress"></a-header>-->
        <my-header :title="'收货地址'" :hasTitle="true" :hasBack="true" :goBack="goBack">
        	<slot name="zx">
        		<div id="add_address" style="padding-right: 0.8rem;width: 30%;text-align: right;position: relative;z-index: 5;" class="aui-pull-right text-del" @click.stop="goAddAddress">新增地址</div>
        	</slot>
        </my-header>
	</div>

</template>

<script>
import MyHeader from "../../header";
import Header from "../../../real-components/AddressHeader";
import {mapGetters,mapMutations} from "vuex";

export default {
  	name: 'hello',
  	data () {
    	return {
			msg: '这是收货地址页面',
            page: "AddressHeader",
            componentName: "AddressHeader",
            dataList: []
		}
	},
	computed: {
		...mapGetters('zx',[
			"getAddressList","getNowAddress"
		]),
		...mapGetters('wxh',[
			'sessionId',
			'userInfo',
		]),
		...mapGetters('xxy', [
				"getaderssgorder"
			])
	},
	components: {
        "a-header": Header,
        "my-header":MyHeader
    },
    methods: {
        ...mapMutations('zx',[
			'setaddFlagMutation',"setNowAddressMutation"
		]),
		...mapMutations('xxy', [
				'setaderssgorder'
			]),
		//判断对象是否为空对象的方法
		checkObj(obj){
			for(let i in obj ){
				return false;
			}
			return true;
		},
		goAddAddress(){
			var obj = null;
			var addressObj = this.getNowAddress;
//			console.log(addressObj,'新增地址的額地址對象')
			var flag = this.checkObj(addressObj);  // 判断addressObj是否为空对象
			if(flag || addressObj.type === 'edit'){
				obj = {
					sex:"男",
					label:"家庭",
					defaultaddress:"非默认",
					phone:'',
					province:'',
					city:'',
					region:'',
					address:'',
					receivername:'',
					result:'',
					houseNumber:'',
					positionlng:'',
					positionlat:'',
					type:''
				}
			}else{
				obj = addressObj;
			}
			
			this.setaddFlagMutation(true);
			this.setNowAddressMutation(obj);
			//存local 防止vuex失效  当失效时取用
			localStorage.setItem('userAddressNow',JSON.stringify(obj));
			let id = this.$route.query.id;
			let pagepath = this.$route.query.pagepath
			if(id){
				this.$router.push({path:'addAddress',query:{id:2}});
			}else if(pagepath){
				this.$router.push({path:'addAddress',query:{id:2.2}});
			}else{
				this.$router.push('addAddress');
			}
		},
		goBack(){
			let id = this.$route.query.id;
			let pagepath = this.$route.query.pagepath 
			if(id){
//				window.history.back();
				var obj = {};
				if(this.userInfo && this.userInfo.addressLists &&  this.userInfo.addressLists.length){
					for(let i = 0; i < this.userInfo.addressLists.length; i++){
						if(this.userInfo.addressLists[i].isNew == 0){
							console.log(this.userInfo.addressLists[i],'返回订单地址')
							obj = this.userInfo.addressLists[i];
						}
					}
					obj.flag = true;
				}else{
					obj.flag = false;
				}
//				console.log(JSON.stringify(obj),'转化JSON')
				if(obj.hasOwnProperty('address')){
					if(id==2.2){
						this.$router.push({path:'ConfirmOrderIntegral',query:{name:JSON.stringify(obj)}});
					}else{
						this.$router.push({path:'confirmOrder',query:{name:JSON.stringify(obj)}});
					}
					
				}else{
					if(id==2.2){
						this.$router.push({path:'ConfirmOrderIntegral'});
					}else{
						this.$router.push({path:'confirmOrder'});
					}
					
				}

			}else if(pagepath){
				//this.$router.push({path:'ConfirmOrderIntegral'});
				window.history.go(-1);
				this.setaderssgorder(this.getaderssgorder-2)
			}else{
				this.$router.push('profile');
			}
		}
    },
	created () {
		
	},
	mounted () {
		// initDomTree(this.getDomTree[this.page]);
	},
	updated() {
		//页面更新完毕
		// initDomTree(this.getDomTree[this.page]);
	}
}
</script>

<!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped>
	.box {
		/* display: flex; */
	}
	.style-components {
		display: flex;
		flex-flow: column nowrap;
	}
	.container {
		width: 800px;
		height: 300px;
		border: 1px solid red;

	}
	.detail{
		width: 50%;
	}
</style>
